//
//  SXStatusViewModel.swift
//  sina
//
//  Created by shengxinwei on 16/5/25.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit

class SXStatusViewModel: NSObject {

    var status: SXStatus?
    

//    ///微博信息内容
//    var text: String?
//    
//    ///微博来源
//    var source: String?

//    
    var repostsText: String?
    
    var commentsText: String?
    
    var attitudesText: String?
    
    var soucreText: String?

    ///转发数
    var reposts_count: Int = 0
    ///评论数
    var comments_count: Int = 0
    ///表态数
    var attitudes_count: Int = 0
    //每次执行
    var time: String? {
        return NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
    }
    
    init(status: SXStatus) {
        super.init()
        self.status = status
        dealSoureText()
        
        repostsText = dealToolBarText(status.reposts_count,defaultName: "转发")
        commentsText = dealToolBarText(status.comments_count, defaultName: "评论")
        attitudesText = dealToolBarText(status.attitudes_count, defaultName: "赞")
        
        
        
    }
    
    //设置toolbar 评论转发显示
    private func dealToolBarText(count: Int,defaultName: String) -> String {
        if count <= 0 {
            return defaultName
        }else if count > 100000 {
            return "\(Double(count/1000)/10)万"
        }
        
        return "\(count)"
    }
    
    private func dealSoureText() {
        //<a href=\"http://app.weibo.com/t/feed/1tqBja\" rel=\"nofollow\">360安全浏览器</a>
        let startString = "\">"
        let endString = "</a>"
        let str = status?.source ?? ""
        let startRange = str.rangeOfString(startString)
        let endRange = str.rangeOfString(endString)
        if let start = startRange , end = endRange {
            let range = start.endIndex..<end.startIndex
             soucreText = str.substringWithRange(range)
        }
    
    }
    
    
}
